package lightswitch.server.code.sqlhelper.statement.delete;

import java.util.ArrayList;
import java.util.List;


import lightswitch.server.code.object.Database;
import lightswitch.server.code.sqlhelper.ExpressionHelper;

import lightswitch.server.code.sqlhelper.expression.Expression;
import lightswitch.server.code.sqlhelper.expression.NormalTable;
import lightswitch.server.code.sqlhelper.statement.Statement;
import lightswitch.server.code.sqlhelper.statement.select.From;
import lightswitch.server.code.util.StringHelper;

public class Delete implements Statement {

	
	From from;
	Expression whereExpression;
	public Delete(String statement)
	{
		
		parse(statement);
	}
	@Override
	public void parse(String statement) {
		
		
		String deleteFromString="";
		String relativeTableString="";
		String whereString="";
		
		// TODO Auto-generated method stub
		statement = StringHelper.NormalizeString(statement);
		
		statement = statement.replace("delete * from", "delete from");
		
		statement = StringHelper.NormalizeString(statement);
		String remainString=statement;
		if(!statement.contains("delete from"))
		{
			//throw exception
			return;
		}
	
		List<String> wordList=new ArrayList<>();
		// lay chuoi set..
		if(!remainString.equals(""))
		{
			
		
		wordList = StringHelper.SplitStringInEqualLevelKeepingToken(remainString);
		wordList.remove(0);
		wordList.remove(0);
		remainString="";
		boolean deleteFromFound = false;
		for(int i=0;i<wordList.size();i++)
		{
			if(deleteFromFound==false)
			{
				if(wordList.get(i).equals("from")||wordList.get(i).equals("where"))
				{
					deleteFromFound =true;
					remainString+=(" "+wordList.get(i));
				}
				else
				{
					deleteFromString+=(" "+wordList.get(i));
				}
			}
			else
			{
				remainString+=(" "+wordList.get(i));
			}
		}
		}
		else
		{
			//ko co update from
			//throw exception
			return;
		}
		
		//lay chuoi from
		if(!remainString.equals(""))
		{
			
		wordList.clear();
		wordList = StringHelper.SplitStringInEqualLevelKeepingToken(remainString);
		remainString="";
		boolean fromfound = false;
		for(int i=0;i<wordList.size();i++)
		{
			if(fromfound==false)
			{
				if(wordList.get(i).equals("where"))
				{
					fromfound =true;
					remainString+=(" "+wordList.get(i));
				}
				else
				{
					relativeTableString+=(" "+wordList.get(i));
				}
			}
			else
			{
				remainString+=(" "+wordList.get(i));
			}
		}
		}
		whereString = remainString;
		

		if(deleteFromString.contains("delete from"))
		{
			deleteFromString = deleteFromString.substring("delete from".length());
		}
		if(relativeTableString.contains("from"))
		{
			relativeTableString = relativeTableString.substring("from ".length());
		}
		if(whereString.contains("where"))
		{
			whereString = whereString.substring("where ".length());
		}
		

		String listTable = "from "+ deleteFromString;// + ", "+ relativeTableString;
		relativeTableString = StringHelper.NormalizeString(relativeTableString);
		
		if(!relativeTableString.equals(""))
			listTable=listTable+", "+relativeTableString;
		from = new From(listTable, null);
		if(whereString=="")
			whereString="true";
		whereExpression = ExpressionHelper.parseExpression(whereString, from);
		
			
	}
	public int execute()
	{
		List<From> list = new ArrayList<>();
		list.add(from);
		
		
		for(int i=0;i<from._tableList.size();i++)
		{
			from._tableList.get(i).InitTable();
		}
		
		NormalTable mainTable = (NormalTable)from._tableList.get(0);
		
		NormalTable _temp=null;
		if(from._tableList.size()>0)
		{
			_temp = (NormalTable) from._tableList.get(0).getAsNormalTable();
		}
		else
			return -1;
		
		for(int i=1;i<from._tableList.size();i++)
		{
			_temp = _temp.MultiWith((NormalTable)from._tableList.get(i),from._joinExpression.get(i));
			
		}
				
		for(int i=0;i<_temp.dataRowList.size();i++)
		{
		
			if(((boolean)whereExpression.GetValue((_temp.dataRowList.get(i)),list))==false)
			{
				_temp.dataRowList.remove(i);
				i--;
			}
			
		}
		
		// bo cac dong cua bang khac
		
		for(int i=mainTable.propertyList.size();i<_temp.propertyList.size();i++)
		{
			_temp.RemoveColumn(i);
			i--;
			
		}
		
		for(int i=0;i<mainTable.dataRowList.size();i++)
		{
			for(int j=0;j<_temp.dataRowList.size();j++)
			{
				if(mainTable.dataRowList.get(i).isLike(_temp.dataRowList.get(j)))
				{
					mainTable.dataRowList.remove(i);
					i--;
					break;
				}
			}
		}
		int rowEff = Database.editTable(mainTable);
		return rowEff;
	}

}
